Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### File Position Every open binary file has its own file pointer. The file pointer is a numeric value that points at the position in the file where read and write operations will occur. Each position in a binary file corresponds to one byte of data. Therefore, a file that contains n bytes has positions numbered from 1 through n. To change the file pointer and determine its value, use the Seek statement and the Seek and Loc functions—the same statements you use when working with random access files. With random access files, the file pointer points at records, not bytes. Visual Basic knows the ACCESS mode in which a given file was opened, and interprets Seek or Loc correctly when you use them—as bytes for a binary file and records for a random file. The syntax of the Seek and Loc statements is
    Seek [#]filenum, newpos
    [pos =] Seek(filenum)
    [pos =] Loc(filenum)
where filenum is the number associated with the file when it was opened in binary mode. The Seek statement sets the file pointer to newpos. The Seek function returns the current position of the file pointer (the next byte to be read or written). The Loc function returns the position of the last byte that was read or written. Unless you explicitly move the pointer, Loc is one less than Seek. #### Reading And Writing Binary File Data To read data from a binary file, use the Get statement. To write data to a binary file, use the Put statement. These are the same statements used for random files; they are interpreted differently, depending on the mode of the file being read or written. The syntax is
    GET [#]filenum, [pos] , var
    PUT [#]filenum, [pos] , var
with the following arguments: • filenum—The number associated with the file when it was opened in binary mode. • pos—Specifies the byte position where the read or write operation is to take place. If pos is omitted, the current file pointer position is used. • var—Any Basic variable. Get reads data from the file into var; Put writes data from var to the file. The statements automatically read or write the number of bytes contained in var. If var is a variable-length string, the number of bytes transferred is equal to the number of characters currently in var. When a binary mode file is first opened, the file pointer is at position one. Any Get or Put operation automatically increments the file pointer by the number of bytes transferred. To determine the length of a binary file, use the Lof function
    [length =] Lof(filenum)
where filenum is the number associated with the file when it was opened in binary mode. The value returned by Lof is equal to the file-pointer position of the file’s last byte. A program can quickly read an entire binary file, using Lof and a loop. The following code shows how to use binary file access to make a copy of a file. It is assumed that ORIGINAL.DAT is an existing file, and that COPY.DAT does not exist. By using a one-character fixed-length string, the program reads and writes one byte at a time. While this code would certainly work, it is inefficient because of the byte-at-a-time approach:
    Dim ch As String * 1, count As Integer, f1 As Integer, F2 As Integer
    f1 = freefile
    Open “ORIGINAL.DAT” For BINARY AS #f1
    f2 = freefile
    Open “COPY.DAT” For BINARY AS #f2

    For count = 1 TO Lof(1)
        Get #f1, , ch
        Put #f2, , ch
    Next count

    Close #f1, #f2
The EOF function—described earlier in the chapter—can be used to detect the end of a binary file. For example, the loop in the previous code could also have been written as follows:
    While Not Eof(f1)
        Get #f1, , ch
        Put #f2, , ch
    Wend
The flexibility of binary files makes them an excellent choice for storing program data, particularly when the data format does not lend itself to a random access or sequential access file. Remember that the unstructured nature of binary files makes it necessary for our program to keep track of the data-storage format. For example, a program that uses an array of 1,000 double-precision values could store the entire array in a binary file as follows:
    Dim data(999) As Double, count As Integer

    Open “ARRAY.DAT” For BINARY As #f1

    For count = 0 To 999
        Put #f1, , data(count)
    Next count

    Close #1
The data could later be retrieved from the disk file back into the array, as follows:
    Open “ARRAY.DAT” For BINARY As #f1

    For count = 0 To 999
        Get #f1, , data(count)
    Next count

    Close #1
Although nothing prevents us from retrieving the data into an array of type Integer or any other Basic data type, the results would be meaningless. Here are a few hints for working with binary files: • To add new data at the end of an existing file, move the file pointer to the end of the file by executing Seek filenum, Lof(filenum)+1. • If you Put data in an existing file at a position before the end of the file, the new data will overwrite existing data. • If you Put data at a file-pointer position past the end of a file (for example, at position Lof(filenum)+10), the file will be extended as needed. The intervening byte positions, however, will contain garbage (that is, undefined values). • If you Get data at a file-pointer position beyond the end of a file, no error occurs, but the returned data will be garbage. ### Which File Type Should You Use? If you are a bit confused by Visual Basic’s different file-access modes, you are not alone. It’s not always clear which type of file is best for a particular application. The following guidelines are designed to help you choose: • For data that consists of lines of text—whether generated by a Visual Basic program or another application—use sequential access mode. You can use either the traditional or FSO programming techniques for sequential text files. • To manipulate nontext files generated by other applications, use binary mode. • To store numerical arrays, use either sequential mode or binary mode. Binary mode is preferred, because it is faster and results in significantly smaller data file sizes. • To store arrays of variable-length strings, use sequential mode. To store arrays of fixed-length strings, use either sequential or binary mode. Binary mode has a size and speed advantage, although not as significant as with numerical arrays. • To store data that is organized in a record and field format, use either sequential or random mode. The final choice depends on the specific needs of the program. • Random access mode provides faster access to individual records in the disk file. It wastes space for string data, because string fields are padded with spaces to fill the allocated field size. • Sequential mode is slower at accessing specific records in the file. It uses space more efficiently, because strings are not padded with spaces. These are just guidelines, not meant to be interpreted as hard-and-fast rules. It’s important to understand that a file’s access mode does not affect the physical file on disk, but only the way a program reads and writes the file data. In other words, any file—regardless of which mode was used to create it—is simply a sequence of bytes stored on the disk. For example, you could create a file using random access mode, then later open and read it in binary access mode. As your programming skills develop, you will sometimes find that mixing file access modes is the preferred approach to your data-storage needs. Until you are quite familiar with all three file access modes, however, I suggest you follow the previous guidelines and limit yourself to one access mode per file.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.